﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using Customer_Profile.eCard.DBDao;
using KVB.Common;
using Customer_Profile.Admin;

namespace Customer_Profile.eCard.CardAccount
{
    public partial class CardAccountingAdd : System.Web.UI.Page
    {
        string strCardno = "";
        string strCardbalance = "";
        string strStatus = "";
        string strMessage = "";

        string strUserName = "";
        string strUserID = "";
        string strEntity = "";

        const string UploadDirectory = "~/eCard/tempUpload/";
        const int ThumbnailSize = 100;
        protected void Page_Load(object sender, EventArgs e)
        {
            Comm.CheckSession(this.Page, "/login.aspx", "usermodel");
            if (Session["usermodel"] != null)
            {
                strUserID = ((UserInfoEntity)Session["usermodel"]).UserLoginId;
                strUserName = ((UserInfoEntity)Session["usermodel"]).UserName;
                strEntity = ((UserInfoEntity)Session["usermodel"]).UserOffice;
            }
            else
            {
                Log.WriteDefaultLog("main_01 Page_Load", "Session is null");
                return;
            }

            if (Request["card_no"] != null)
            {
                strCardno = Request["card_no"].ToString().Trim();
                txtCardno.Text = strCardno;
            }
            if (!Page.IsPostBack)
            {
                bindBalance();
                bindCflowType();
                bindDeals(strCardno);
            }
        }

        private void bindBalance()
        {
            AccountingDao.getCardBalance(strCardno, out strCardbalance, out strStatus, out strMessage);
            if (strStatus.Equals("N"))
            {
                lblWarning.Text = strMessage;
            }
            if (strCardbalance.Equals(""))
            {
                card_balance.Text = "不能查询到余额，请联系管理员";
                btnAction.Enabled = false;
                lblWarning.Text = strMessage;
            }
            else
            {
                card_balance.Text = strCardbalance;
            }
        }

        private void bindDeals(string card_no)
        {
            DataSet ds = AccountingDao.getDealsbyCardno(card_no);
            if (ds.Tables[0].Rows.Count > 0)
            {
                gridDeal.DataSource = ds;
                gridDeal.DataBind();
                gridDeal.Selection.SelectRow(0);
            }

        }

        private void bindCflowType()
        {
            CodeDao codeDeoobject = new CodeDao();
            DataSet ds = codeDeoobject.GetList("9");
            rbCflowType.DataSource = ds;
            rbCflowType.DataTextField = "CodeName";
            rbCflowType.DataValueField = "CodeValue";
            rbCflowType.DataBind();

            if (ds.Tables[0].Rows.Count > 0)
            {
                rbCflowType.SelectedIndex = 0;
            }
        }

        protected void btnAction_Click(object sender, EventArgs e)
        {
            // 0,检查余额是否足够
            // 1,添加cflow数据
            // 2,更新card_balance 表里面的卡内余额
            // 3,更新mmdeals里面的deal_balance的余额            
            try
            {
                string attach_url = uploadAttach();
                int ideal_no = int.Parse(hidDealno.Value.ToString());
                decimal dcflow_amount = decimal.Parse(txtAmount.Text.Trim());
                AccountingDao.cardRecharge(strCardno, ideal_no, rbCflowType.SelectedItem.Value, dcflow_amount, txtRemark.Text,attach_url, strUserID, strUserName, out strStatus, out strMessage);
                if (strStatus.Equals("N"))
                {
                    lblWarning.Text = strMessage;
                }
                else
                {
                    lblWarning.Text = "充值成功！";
                    bindBalance();
                }
            }
            catch (Exception ex)
            {
                lblWarning.Text = ex.ToString();
            }
        }

        private string uploadAttach()
        {
            string view_name = "";
            string attach_name = "";
            string attach_type = "";
            string attach_size = "";
            string attach_url = "";
            try
            {
                if (FileUpload1.HasFile)
                {
                    FileInfo fileInfo = new FileInfo(FileUpload1.FileName);
                    attach_name = DateTime.Now.ToString("yyyyMMddHHmmssfff") + fileInfo.Name;
                    string savePathName = MapPath(UploadDirectory) + attach_name;
                    FileUpload1.SaveAs(savePathName);

                    view_name = fileInfo.Name;
                    attach_type = FileUpload1.PostedFile.ContentType.ToString();
                    attach_size = FileUpload1.PostedFile.ContentLength / 1024 + "K";
                    attach_url = "/eCard/tempUpload/" + attach_name;
                }
            }
            catch (Exception ex)
            {
                lblWarning.Text = "上传附件失败" + ex.ToString();
            }
            return attach_url;
        }
    }
}